const enterpriseVue = new Vue({
  el: '#app',
  data() {
    return {
      pickerOptions: pickerOptions,
      rules: {
      
      },
      column:[],
      shop:[],
      store:[],
      isDisabled:false,
      type:'',
      form: {
        Sales_Unit_Price_SerialNumber: '',
        Start_Date: '',
        End_Date: '',
        ShopColumn_Id: '',
        Row_Status: '',
        Operation_ID: '',
        Operation_Datetime: '',
        Children: {
          sales_unitprice_detail: [{
            Sales_Unit_Price_Detail_SerialNumber: '',
            Sales_Unit_Price_SerialNumber: '',
            Article_SerialNumber: '',
            Unit_Price: '',
            Sales_Unit_Price: '',
            Row_Status: '',
            Operation_ID: '',
            Operation_Datetime: '',
          }],
          sales_unitprice_storescope: [{
            Sales_Unit_Price_StoreScope_SerialNumber: '',
            Sales_Unit_Price_SerialNumber: '',
            Store_SerialNumber: '',
            Row_Status: '',
            Operation_ID: '',
            Operation_Datetime: '',
          }]
        }
      },
      loading: false
    }
  },
  mounted() {
    const Sales_Unit_Price_SerialNumber = sessionStorage.getItem("Sales_Unit_Price_SerialNumber")
    this.getData(Sales_Unit_Price_SerialNumber)
  },
  methods: {
    getData(Sales_Unit_Price_SerialNumber) {
      const _this = this
      $.ajax({
        url: DomainName + '/SalesUnitprice_Edit',
        type: 'POST',
        data: { Data: Sales_Unit_Price_SerialNumber ,token:$.cookie("token")},
        success: function (res) {
          console.log(res);
          _this.form = res.sales_unitprice[0]
          if (!_this.form.Children.sales_unitprice_detail || _this.form.Children.sales_unitprice_detail.length == 0) {
            _this.form.Children.sales_unitprice_detail = [{
              Sales_Unit_Price_Detail_SerialNumber: '',
              Sales_Unit_Price_SerialNumber: this.form.Sales_Unit_Price_SerialNumber,
              Article_SerialNumber: '',
              Unit_Price: '',
              Sales_Unit_Price: '',
              Row_Status: '',
              Operation_ID: '',
              Operation_Datetime: '',
            }]
          }
          if (!_this.form.Children.sales_unitprice_storescope || _this.form.Children.sales_unitprice_storescope.length == 0) {
            _this.form.Children.sales_unitprice_storescope = [{
              Sales_Unit_Price_StoreScope_SerialNumber: '',
              Sales_Unit_Price_SerialNumber:this.form.Sales_Unit_Price_SerialNumber,
              Store_SerialNumber: '',
              Row_Status: '',
              Operation_ID: '',
              Operation_Datetime: '',
            }]
          }
        }
      })
      $.ajax({
        url: DomainName + '/StoreInfo',
        type: 'GET',
        data: { action: "jQueryAjax" },
        success: function (res) {
          res = res.filter(function (item) {
            return item.Row_Status != '1'
          })
          for (let i = 0; i < res.length; i++) {
            _this.store.push(res[i]);
          }  
        }
      })
      $.ajax({
        url: DomainName + '/ShopColumn',
        type: 'GET',
        data: { action: "jQueryAjax" },
        success: function (res) {
          res = res.filter(function (item) {
            return item.Row_Status != '1'
          })
          for (let i = 0; i < res.length; i++) {
            _this.column.push(res[i]);
          }  
        }
      })
      $.ajax({
        url: DomainName + '/ArticleInfo',
        type: 'GET',
        data: { action: "jQueryAjax" },
        success: function (res) {
          res = res.filter(function (item) {
            return item.Row_Status != '1'
          })
          for (let i = 0; i < res.length; i++) {
            _this.shop.push(res[i]);
          }  
        }
      })
    },
    submitForm(formName) {
      let _this = this
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const { add, modify } = _this.deal()
          let data = {}
          if (add == null) {
            data = { Data:JSON.stringify({Modify: { sales_unitprice: [modify] }}) }
          } else {
            data = { Data:JSON.stringify(
              { 
                Modify:{ sales_unitprice: [modify] },
                Add:{ sales_unitprice: [add] }
              }
            )}
          }
          _this.loading = true
          $.ajax({
            url: DomainName + '/GoodsFileEdit',
            type: 'POST',
            data: data,
            success: function (res) {
              console.log('提交结果', res)
              _this.$message.success("提交成功")
              window.history.go(-1);
            },
            error: function () {
              _this.$message.error("操作失败，请稍后再试")
            },
            complete: function () { _this.loading = false }
          })
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    deal() {
      const add = deepClone(this.form)
      const modify = deepClone(this.form)
      let add_enterprise_legal_person = [], modify_enterprise_legal_person = []
      let add_series = [], modify_series = []
      for (const i in modify.Children.enterprise_legal_person) {
        const obj = modify.Children.enterprise_legal_person[i];
        if (!Object.keys(obj).some(item => item.includes('_Original'))) {
          add_enterprise_legal_person.push(obj)
        } else {
          modify_enterprise_legal_person.push(obj)
        }
      }
      for (const i in modify.Children.series) {
        const obj = modify.Children.series[i];
        if (!Object.keys(obj).some(item => item.includes('_Original'))) {
          add_series.push(obj)
        } else {
          modify_series.push(obj)
        }
      }

      add.Children.enterprise_legal_person = add_enterprise_legal_person
      add.Children.series = add_series

      modify.Children.enterprise_legal_person = modify_enterprise_legal_person
      modify.Children.series = modify_series

      if (add_enterprise_legal_person.length == 0 && add_series.length == 0) {
        return { add: null, modify };
      }
      return { add, modify };
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    Change_Unit_Price(index){
      this.isDisabled = true
      let str = this.form.Children.sales_unitprice_detail[index].Article_SerialNumber
      for (let i = 0; i < this.shop.length; i++) {
        if (this.shop[i].Article_SerialNumber == str) {
          this.form.Children.sales_unitprice_detail[index].Unit_Price = this.shop[i].Unit_Price
        }
      }
    },
    add_sales_unitprice_storescope() {
      this.form.Children.sales_unitprice_storescope.push({     //销售价格单适用店铺
        Sales_Unit_Price_StoreScope_SerialNumber: "",
        Sales_Unit_Price_SerialNumber: '',
        Store_SerialNumber: '',
      })
    },
    remove_sales_unitprice_storescope() {
      if (this.form.Children.sales_unitprice_storescope.length > 1) {
        this.form.Children.sales_unitprice_storescope.pop()
      } else {
        this.$message({
          message: '不能在删除了',
          type: 'warning'
        })
      }
    },
    add_sales_unitprice_detail() {
      this.form.Children.sales_unitprice_detail.push({     //销售价格单明细
        Sales_Unit_Price_Detail_SerialNumber: "",
        Sales_Unit_Price_SerialNumber: '',
        Article_SerialNumber: '',
        Unit_Price: '',
        Sales_Unit_Price: '',
      })
    },
    remove_sales_unitprice_detail() {
      if (this.form.Children.sales_unitprice_detail.length > 1) {
        this.form.Children.sales_unitprice_detail.pop()
      } else {
        this.$message({
          message: '不能在删除了',
          type: 'warning'
        })
      }
    }
  }
})